package day23;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Homework4 {
    public static void main(String[] args) {
        File file=new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\my.txt");
        File newfile=new File("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\result.txt");
        Map<Character,Integer> map=new HashMap<>();
        if (file.exists()){
            FileInputStream fis=null;
            FileOutputStream fos = null;
            try {
                fis=new FileInputStream(file);
                fos = new FileOutputStream(newfile);
                byte bs[]=new byte[1024];
                int len;
                int sum=0;
                String str="";
                while ((len=fis.read(bs))!=-1){
                    for (int i=0;i<bs.length;i++){
                        for (int j=0;j<bs.length;j++){
                            if (bs[i]==bs[j]){
                                sum++;
                                Character c= (char)bs[i];
                                map.put(c,sum);
                            }
                        }
                        sum=0;
                    }
                    for (Character key:map.keySet()){
                        int value=map.get(key);
                        str+=key+"=";
                        str+=value+" ";
                    }
                    System.out.println(str);
                    fos.write(str.getBytes());
                    System.out.println("存入成功！");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("该文件不存在!");
        }
    }
}
